/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @TaskScrollView.java  2014年9月16日 下午4:59:26 - Guzhu
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.android.tiango.baba.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ScrollView;

import com.android.tiango.baba.util.BuileGestureExt;
import com.android.tiango.baba.util.ToastUtils;

public class TaskScrollView extends ScrollView {

	private List<ScrollListener> mListeners = new ArrayList<TaskScrollView.ScrollListener>();
	private int state = 0;
	public static final int UP = 1;
	public static final int DOWN = 2;
	public static final int STOP = 3;
	private float y = 0;
	private View inner;
	private Rect normal = new Rect();;
	private ScrollTopListener scrollTopListener;
	private MyTaskTopView myTaskTopView;
	private GestureDetector gestureDetector;

	public MyTaskTopView getMyTaskTopView() {
		return myTaskTopView;
	}

	public void setMyTaskTopView(MyTaskTopView myTaskTopView) {
		this.myTaskTopView = myTaskTopView;
	}

	public interface ScrollListener {
		void onScrollChanged(int state, int t);
	}

	public interface ScrollTopListener {
		void onScrollChanged(boolean isUp);
	}

	public TaskScrollView(Context context) {
		this(context, null);
	}

	public TaskScrollView(Context context, AttributeSet set) {
		this(context, set, 0);
	}

	public TaskScrollView(Context context, AttributeSet set, int defStyle) {
		super(context, set, defStyle);
		initView(context);
	}

	private void initView(Context context) {
		gestureDetector = new BuileGestureExt(context, new BuileGestureExt.OnGestureResult() {
			@Override
			public void onGestureResult(int direction) {
				 show(Integer.toString(direction));
			}
		}).Buile();

	}

	public void AddListener(ScrollListener listener) {
		if (mListeners == null)
			mListeners = new ArrayList<TaskScrollView.ScrollListener>();
		mListeners.add(listener);
	}

	public ScrollTopListener getScrollTopListener() {
		return scrollTopListener;
	}

	public void setScrollTopListener(ScrollTopListener scrollTopListener) {
		this.scrollTopListener = scrollTopListener;
	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		if (t > oldt) {
			state = UP;
		} else if (t < oldt) {
			state = DOWN;
		} else {
			state = STOP;
		}
		sendScroll(state, t);
	}

	public void sendScroll(int state, int scroll) {
		for (ScrollListener listener : mListeners) {
			listener.onScrollChanged(state, scroll);
		}
	}

	@Override
	protected void onFinishInflate() {
		if (getChildCount() > 0) {
			inner = getChildAt(0);
		}
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		boolean ss = super.onInterceptTouchEvent(ev);
//		System.out.println("ss:"+ss);
		return false;
	}
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		// if (inner == null) {
		// return super.onTouchEvent(ev);
		// } else {
		 return gestureDetector.onTouchEvent(ev);
//		if (null != myTaskTopView) {
//			if (myTaskTopView.isShow) {
//				commOnTouchEvent(ev);
//				return false;
//			} else {
//				commOnTouchEvent(ev);
//				return super.onTouchEvent(ev);
//			}
//		} else {
//			return super.onTouchEvent(ev);
//		}
	}

	public void commOnTouchEvent(MotionEvent ev) {
		int action = ev.getAction();
		boolean isUp = false;
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			y = ev.getY();
			System.out.println("ACTION_DOWN:" + y);

			break;
		case MotionEvent.ACTION_UP:
			// if (isNeedAnimation()) {
			// animation();
			// }
			y = 0;
			break;
		case MotionEvent.ACTION_MOVE:
			final float preY = y;
			float nowY = ev.getY();
			int deltaY = (int) (preY - nowY);
			// 滚动
			// scrollBy(0, deltaY);
			System.out.println("deltaY:" + deltaY);
			if (deltaY > 10 && deltaY < 200) {
				isUp = true;
			} else if (deltaY < -50 && deltaY > -200) {
				isUp = false;
			} else {
			}
			y = nowY;
			// 当滚动到最上或者最下时就不会再滚动，这时移动布局
			System.out.println("isUp:" + isUp + " deltaY:" + deltaY);
			if (isNeedMove() || myTaskTopView.isShow) {
				// if (normal.isEmpty()) {
				// // 保存正常的布局位置
				// normal.set(inner.getLeft(), inner.getTop(), inner.getRight(), inner.getBottom());
				//
				// }
				if (null != scrollTopListener) {
					scrollTopListener.onScrollChanged(isUp);
				}
				// // 移动布局
				// inner.layout(inner.getLeft(), inner.getTop() - deltaY, inner
				// .getRight(), inner.getBottom() - deltaY);
			}
			break;
		default:
			break;
		}
	} // 开启动画移动

	public void animation() {
		// 开启移动动画
		TranslateAnimation ta = new TranslateAnimation(0, 0, inner.getTop(), normal.top);
		ta.setDuration(200);
		inner.startAnimation(ta);
		// 设置回到正常的布局位置
		inner.layout(normal.left, normal.top, normal.right, normal.bottom);
		normal.setEmpty();
	}

	// 是否需要开启动画
	public boolean isNeedAnimation() {
		return !normal.isEmpty();
	}

	// 是否需要移动布局
	public boolean isNeedMove() {
		// int offset = 0;
		// if (null != myTaskTopView) {
		// offset = myTaskTopView.getMeasuredHeight() - getHeight();
		// }
		int scrollY = getScrollY();
		System.out.println(" scro:" + scrollY);
		// if (scrollY == 0 || scrollY == offset) {
		if (scrollY == 0) {
			return true;
		}
		return false;
	}
	
	private void show(String value){
        ToastUtils.showMessage(value);
    }

}
